package com.achievement.entity.dto;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.ListJoin;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Subquery;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.data.jpa.domain.Specification;

import com.achievement.entity.Course;
import com.achievement.entity.Subject;
import com.user.entity.Student;

public class CourseDTO {
	public static Specification<Course> getSpecification(Long studentId,boolean isSelected) {
		Specification<Course> spec = new Specification<Course>() {
			public Predicate toPredicate(Root<Course> root, CriteriaQuery<?> query, CriteriaBuilder cb) {

				DetachedCriteria criteria = DetachedCriteria.forClass(Course.class);
				criteria.createAlias("students", "s").add(Restrictions.ne("s.id", studentId));
				
				List<Predicate> list = new ArrayList<Predicate>();
				if(studentId!=null&&isSelected) {
					ListJoin<Course,Student> join= root.join(root.getModel().getList("students",Student.class),JoinType.LEFT);
					Predicate predicate = cb.equal(join.get("id").as(Long.class),studentId);
					list.add(predicate);
				}else if(studentId!=null&&!isSelected) {
					System.out.println("查询待选课程");
					
					//Predicate predicate = cb.notEqual(join.get("id").as(Long.class), studentId);
					//Predicate predicate = cb.or(cb.isNull(join),cb.notEqual(join.get("id").as(Long.class), studentId));
					//Predicate predicate = cb.isNull(join);
					//Predicate predicate = cb.in(cb.equal(join.get("id").as(Long.class),studentId));
					Subquery<Course> subquery = query.subquery(Course.class);
					Root<Course> subRoot = subquery.from(Course.class);
					ListJoin<Course,Student> join= subRoot.join(root.getModel().getDeclaredList("students", Student.class),JoinType.LEFT);
					subquery.select(subRoot);
					
					Predicate p = cb.equal(join.get("id").as(Long.class), studentId);
					subquery.where(p);
					Predicate predicate = cb.not(cb.in(subquery));
					list.add(predicate);
				}else {
					
				}
				
				
				Predicate[] p = new Predicate[list.size()];
				return cb.and(list.toArray(p));
			}
		};
		return spec;
	}
	public static Specification<Course> getFindPageSpec(String str,Long subjectId) {
		Specification<Course> spec = new Specification<Course>() {
			public Predicate toPredicate(Root<Course> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				
				List<Predicate> list = new ArrayList<Predicate>();
				List<Predicate> result_list = new ArrayList<Predicate>();
				Predicate[] p = new Predicate[list.size()];
				if(null!=str&&!"".equals(str)) {
					Predicate  predicate1 =  cb.like(root.get("name").as(String.class),"%"+str+"%");
					list.add(predicate1);
					Predicate  predicate2 =  cb.like(root.get("num").as(String.class),"%"+str+"%");
					list.add(predicate2);

					Predicate  result =  cb.or(list.toArray(p));
					result_list.add(result);
				}
				if(null!=subjectId) {
					Join<Course, Subject> join = root.join(root.getModel().getSingularAttribute("subject",Subject.class),JoinType.LEFT);
					Predicate  predicate =  cb.equal(join.get("id").as(Long.class),subjectId);
					result_list.add(predicate);
				}
				
				return cb.and(result_list.toArray(p));
			}
		};
		return spec;
	}
}
